interface NameInfo{
  firstName: string;
  lastName: string;
  name?: string ;  //   
}

// ?: 可选参数

const getFullName = ({firstName, lastName}: NameInfo): string => {
  return `${firstName} ${lastName}`
}

interface Vegrtable {
  color?: string;
  readonly type: string;  // 只读元素，在第一次赋值之后不允许再次赋值
  [prop: string]: any;  // 多余参数处理第二种方法
}

const getVegetables = ({color, type}: Vegrtable) => {
  return `A ${color ? (color + ' ') : ''}${type}`
}

// console.log(getVegetables({  
//   type: 'tomato',
//   color: 'red',
//   size: 2,
// } as Vegrtable)); // 第一种

const vegetableInfo = { 
  type: 'tomato',
  color: 'red',
  size: 2,
}
console.log(getVegetables(vegetableInfo));    // 第三种方法

// 多余参数的处理 1. 使用类型断言， 2. 索引名, 3.类型兼容性

let vegetableObj: Vegrtable = {
  type: 'tomato'
}

// vegetableObj.type = 'carrot'

interface ArrInter {
  0: number;
  1: string;
}

let arr: ArrInter = [111, 'aaa']


interface AddFunc {
  (num1: number, num2: number): number
}

// type AddFunc = (num1: number, num2: number) => number  和上面的定义一样
const add: AddFunc = (n1, n2) => n1 + n2

add(3, 5)

// interface RoleDic {
//   [id: number]: string
// }

// const role1: RoleDic = {
//   0: 'adb'
// }

interface RoleDic {
  [id: string]: string
}

const role2: RoleDic = {
  a: 'super_admin',
  1: 'admin'   // 默认会把数字转换成字符串
}

interface Vegetables {
  color: string
}

// extends 继承

interface Tomato extends Vegetables {
  radius: number
}

interface Carrot extends Vegetables{
  length: number
}

const tomato: Tomato = {
  radius: 1,
  color: 'red'
}

const carrot: Carrot = {
  length: 2,
  color: 'blue'
}

// 混合类型接口
interface Counter {
  (): void;
  count: number;
}

const getCounter = (): Counter => {
  const c = () => {c.count++ }
  c.count = 0
  return c
}

const counter: Counter = getCounter()

counter()
console.log(counter.count);
counter()
console.log(counter.count);
counter()
console.log(counter.count);

